import smtplib
import string
import re
import pepe.txt.bigtxt as bt

def mail(serverURL=None, sender='', to='', subject='', text=''):
    """
    Usage:
    mail('somemailserver.com', 'me@example.com', 'someone@example.com', 'test', 'This is a test')
    """
    headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to, subject)
    message = headers + text
    mailServer = smtplib.SMTP(serverURL)
    mailServer.sendmail(sender, to, message)
    mailServer.quit()

mbody = open(u'mail-body.txt','r').read()
msubject = open(u'mail-subject.txt','r').read()
#mbody = open(u'plainbody.txt','r').read()
#msubject = open(u'plainsubject.txt','r').read()

def mailer_log(lfile, msg):
    log = open(lfile,'a')
    log.write(msg)
    log.close()

lino = 0
for distl in bt.lines('dist-list.txt'):
    lino += 1
    distl = re.sub('\n','',distl)
    #try:
    token,recipient,fullname = string.split(distl,'|')
    print recipient
    #print token,recipient,fullname
    mailer_log('mailer.log', str(lino)+"<lino-OK-line>"+distl+'\n')
    mail(   serverURL='hubud0410.atrema.deloitte.com',
            sender="\"Marinov, Marian \(Deloitte\)\" <mmarinov@deloittece.com>",
            to=recipient,
            subject=msubject,
            text=mbody)
    """
    except:
        print 'Exception occured: '+str(lino)+' - '+distl
        mailer_log('mailer.log', str(lino)+"<lino-NOK-line>"+distl+'\n')
    """

#hubud0410.atrema.deloitte.com
#smtp.etmail.cz
#"\"Marinov, Marian \(Deloitte\)\" <vpresovsky@deloittece.com>"
#"Marinov Marian (Deloitte) <presovsky@gmail.com>"
